package com.himore.api.web.enums;


import com.alibaba.fastjson.JSONObject;

public enum CodeType {
    SUCCESSFUL(2000),

    FAILED(2100);

    public static final String STATUS = "status";

    public static final String MW_SUCC_STATUS = "2000";

    private final int code;

    CodeType(int value) {
        code = value;
    }

    public int getValue() {
        return code;
    }

    public String getCodeJson() {
        return CodeType.getCodeJson(this);
    }

    public JSONObject getJsonObject() {
        return JSONObject.parseObject(CodeType.getCodeJson(this));
    }

    public static boolean isSuccessful(JSONObject retJson) {
        if (CodeType.MW_SUCC_STATUS.equals(retJson.getString("status"))) {
            return true;
        }
        return false;
    }

    public static String buildSuccRetJson(Object data) {
        JSONObject ret = JSONObject.parseObject(SUCCESSFUL.getCodeJson());
        ret.put("data", data);
        return ret.toString();
    }

    public static JSONObject buildSuccJson(String key, Object value) {
        JSONObject succJson = JSONObject.parseObject(SUCCESSFUL.getCodeJson());
        succJson.put(key, value);
        return succJson;
    }

    public static JSONObject buildSuccJson(Object data) {
        JSONObject succJson = JSONObject.parseObject(SUCCESSFUL.getCodeJson());
        succJson.put("data", data);
        return succJson;
    }

    public static JSONObject buildSuccJson() {
        return JSONObject.parseObject(SUCCESSFUL.getCodeJson());
    }

    public static JSONObject buildFailedJson() {
        return JSONObject.parseObject(FAILED.getCodeJson());
    }

    public static JSONObject buildSuccJson(Object data, String key, Object value) {
        JSONObject succJson = JSONObject.parseObject(SUCCESSFUL.getCodeJson());
        succJson.put("data", data);
        succJson.put(key, value);
        return succJson;
    }

    public static JSONObject buildFailedRetJson(Object data) {
        JSONObject failedJson = JSONObject.parseObject(FAILED.getCodeJson());
        failedJson.put("data", data);
        return failedJson;
    }

    public static JSONObject buildFailedRetJson(Object data, String key, Object value) {
        JSONObject failedJson = JSONObject.parseObject(FAILED.getCodeJson());
        failedJson.put("data", data);
        failedJson.put(key, value);
        return failedJson;
    }

    public static String buildFailedRetStr(Object data, CodeType codeType) {
        JSONObject failedJson = JSONObject.parseObject(codeType.getCodeJson());
        failedJson.put("data", data);
        return failedJson.toString();
    }

    public static JSONObject buildFailedRetJson(Object data, CodeType codeType) {
        JSONObject failedJson = JSONObject.parseObject(codeType.getCodeJson());
        failedJson.put("data", data);
        return failedJson;
    }


    public static String getCodeJson(CodeType codeType) {
        switch (codeType) {
            case SUCCESSFUL:
                return "{\"status\": \"2000\",\"code\": \"2000\",\"msg\": \"Successful!\"}";
            default:
                return "{\"status\": \"2100\",\"code\": \"2100\",\"msg\": \"UNKNOW ERROR!\"}";
        }
    }

    public static CodeType findByValue(int value) {
        switch (value) {
            case 2000:
                return SUCCESSFUL;
            default:
                return FAILED;
        }
    }
}
